using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

using Walkmen.OpenMG;
using Walkmen.Util;

namespace Walkmen
{
    public class Song
    {
        private string _artist;
        private string _album;
        private string _title;
        private int _track;

        public string Artist
        {
            get { return _artist; }
            set { _artist = value; }
        }

        public string Album
        {
            get { return _album; }
            set { _album = value; }
        }

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public int Track
        {
            get { return _track; }
            set { _track = value; }
        }

        public static Song Parse(string fileName)
        {
            using (Stream stream = new BufferedStream(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                OMAHeader omaHeader = OMAHeader.Parse(stream);
                if (omaHeader.FileType != "ea3")
                {
                    throw new FormatException();
                }

                OpenMGHeader omg = OpenMGHeader.Parse(stream);
                if (omg.FileType != "EA3")
                {
                    throw new FormatException();
                }

                if (omg.CodecType != CodecType.MP3)
                {
                    throw new NotSupportedException();
                }

                return Parse(omaHeader.Frames);
            }
        }

        public static Song Parse(IList<OMAFrame> frames)
        {
            Song song = new Song();
            foreach (OMAFrame frame in frames)
            {
                switch (frame.FrameId)
                {
                    case OMATextFrame.FRAME_ID_TPE1:
                        song.Artist = ((OMATextFrame)frame).Value;
                        break;
                    case OMATextFrame.FRAME_ID_TIT2:
                        song.Title = ((OMATextFrame)frame).Value;
                        break;
                    case OMATextFrame.FRAME_ID_TALB:
                        song.Album = ((OMATextFrame)frame).Value;
                        break;
                    case OMATextFrame.FRAME_ID_TRCK:
                        song.Track = Int32.Parse(((OMATextFrame)frame).Value.Split('/')[0]);
                        break;
                    case OMATXXXFrame.FRAME_ID:
                        OMATXXXFrame user = (OMATXXXFrame)frame;
                        if (user.Description == OMATXXXFrame.DESCRIPTION_TRACK)
                        {
                            if (song.Track == 0)
                            {
                                song.Track = Int32.Parse(user.Value);
                            }
                        }
                        else if (user.Description == OMATXXXFrame.DESCRIPTION_TPE1S)
                        {
                            if (String.IsNullOrEmpty(song.Artist))
                            {
                                song.Artist = user.Value;
                            }
                        }
                        else if (user.Description == OMATXXXFrame.DESCRIPTION_TIT2S)
                        {
                            if (String.IsNullOrEmpty(song.Title))
                            {
                                song.Title = user.Value;
                            }
                        }
                        break;
                }
            }
            return song;
        }
    }
}
